package com.andy.ui.base;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andy.ui.R;
import com.just.agentweb.AgentWeb;

/**
 * @Author: luofan
 * @CreateDate: 2020/3/28 15:40
 * @Description:
 */
public class SimpleWebActivity extends BaseActivity {

    private static final String WEB_URL = "webUrl";
    private static final String WEB_TITLE = "webTitle";

    ImageView mImgBack;

    TextView mTvTitleCenter;

    TextView mTvTitleRight;

    ImageView mImgRightBtn;

    RelativeLayout mLayoutTitle;

    FrameLayout mWebContentLayout;

    private String mWebUrl;

    private String mTitle;

    private AgentWeb mAgentWeb;

    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_webview;
    }

    @Override
    public void initView() {
        mImgBack = findViewById(R.id.img_back);
        mTvTitleCenter = findViewById(R.id.tv_title_center);
        mTvTitleRight = findViewById(R.id.tv_title_right);
        mImgRightBtn = findViewById(R.id.img_right);
        mLayoutTitle = findViewById(R.id.layout_title);
        mWebContentLayout = findViewById(R.id.web_content_layout);
        mWebUrl = getIntent().getStringExtra(WEB_URL);
        mTitle = getIntent().getStringExtra(WEB_TITLE);
    }

    @Override
    public void initLogic() {
        mImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mAgentWeb.back()) {
                    finish();
                }
            }
        });
        mTvTitleCenter.setText(mTitle);
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(mWebContentLayout, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready().go(mWebUrl);
    }

    public static void startMe(Context context, String url, String title) {
        Intent intent = new Intent(context, SimpleWebActivity.class);
        intent.putExtra(WEB_URL, url);
        intent.putExtra(WEB_TITLE, title);
        context.startActivity(intent);
    }
}
